//
//  SSNormalMealCell.swift
//  拾舍清洁
//
//  Created by sky.wei on 2023/6/5.
//

import UIKit

class SSNormalMealCell: UITableViewCell {

    @IBOutlet weak var white_card: UIView!
    
    @IBOutlet weak var imageV: UIImageView!
    
    @IBOutlet weak var tagLab: UILabel!
    
    @IBOutlet weak var titleLab: UILabel!
    
  
    @IBOutlet weak var tagLab2: UILabel!
    
    @IBOutlet weak var priceLab: UILabel!
    
    @IBOutlet weak var mealView: SSMealView!
    
    @IBOutlet weak var mealView2: SSMealView!
    
    @IBOutlet weak var mealView3: SSMealView!
    
    var lastMealView = SSMealView()
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        contentView.backgroundColor = UIColor(hex: "1776CA")
        //套餐回调
        mealView.currentTag = 0
        mealView.mealResult { contentView in
            self.handleMeal(contentView: contentView)
        }
        
        mealView2.currentTag = 1
        mealView2.mealResult { contentView in
            self.handleMeal(contentView: contentView)
        }
        
        mealView3.currentTag = 2
        mealView3.mealResult { contentView in
            self.handleMeal(contentView: contentView)
        }
        
    }
    
    func handleMeal(contentView:SSMealView) {
        
        let lastMeal = normal.listSpecs[self.lastMealView.currentTag]
        lastMeal.isSelected = false
        print("lastTag:\(lastMealView.currentTag)")
        
        let meal =  normal.listSpecs[contentView.currentTag]
        meal.isSelected = true
        print("currentTag:\(contentView.currentTag)")
   
        priceLab.text = "$\(meal.specsPrice)"
//        normalPrice(label: priceLab, price: meal.specsPrice)
        
        self.lastMealView.layer.borderWidth = 1
        self.lastMealView.layer.borderColor = UIColor(hex: "#F3F3F3")?.cgColor
        self.lastMealView.backgroundColor = UIColor.white
        self.lastMealView.topTextLab.textColor = UIColor.black
        self.lastMealView.bottomTextLab.textColor = UIColor(hex: "#3D3D3D")
        
        contentView.layer.borderWidth = 2
        contentView.layer.borderColor = UIColor(hex: "#197FD6")?.cgColor
        contentView.backgroundColor = UIColor(hex: "E7F2FA")
        contentView.topTextLab.textColor = UIColor(hex: "103573")
        contentView.bottomTextLab.textColor = UIColor(hex: "103573")
        
        self.lastMealView = contentView
        
  
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

    }
    
    func setCorner() {
        white_card.layer.cornerRadius = 12
        white_card.layer.maskedCorners = [.layerMinXMaxYCorner,.layerMaxXMaxYCorner]
    }
    
    func cancelCorner() {
        white_card.layer.cornerRadius = 0
        white_card.layer.maskedCorners = [.layerMinXMaxYCorner,.layerMaxXMaxYCorner]
    }
    
    var normal: SSNormal = SSNormal() {
        didSet {
      
            tagLab.text = normal.pkgType
            imageV.setImage(with: normal.pkgImg)
            guard let price = normal.listSpecs.first?.specsPrice else{
                return
            }
            priceLab.text = "$\(price)"
//            normalPrice(label: priceLab, price: price)
            tagLab2.text = splitStr(datas: normal.listTag)
            
            //标题处理
            let str2:NSString = normal.pkgName as NSString
            let range =  str2.range(of: normal.keyword)
            
            let str = stringInsert(string: normal.pkgName, keyword: normal.keyword, insertStr: " ")
            normalStrToTitle(textLab: titleLab, string: str, range: range)
            
            //套餐规格
            freshSpecs()
        }
    }
    
    //
    func freshSpecs() {
        if(normal.listSpecs.count < 3){
            return
        }
        let meal =  normal.listSpecs[0]
        let meal1 =  normal.listSpecs[1]
        let meal2 =  normal.listSpecs[2]
        mealView.topTextLab.text = meal.specsName
        mealView.bottomTextLab.text = meal.specsDescribe
        if(meal.isSelected){
            handleMeal(contentView: mealView)
        }
        mealView2.topTextLab.text = meal1.specsName
        mealView2.bottomTextLab.text = meal1.specsDescribe
        if(meal1.isSelected){
            handleMeal(contentView: mealView2)
        }
        mealView3.topTextLab.text = meal2.specsName
        mealView3.bottomTextLab.text = meal2.specsDescribe
        if(meal2.isSelected){
            handleMeal(contentView: mealView3)
        }
    }
    
    ///进入详情
    @IBAction func detailAction(_ sender: Any) {
        let vc = SSNormalDetailController()
        vc.normal = normal
        vc.callback {
            self.freshSpecs()
        }
        UIViewController.current().navigationController?.pushViewController(vc, animated: true)
    }
}


func normalPrice(label:UILabel,price:Float) {
    let attributedString2 = NSMutableAttributedString(string:"券后价:$\(price)")
    attributedString2.addAttribute(NSAttributedString.Key.font, value: UIFont.boldSystemFont(ofSize: 14), range: NSRange(location: 0, length: 4))
    label.attributedText = attributedString2
}
